// This code is copied from CodeProject.com
// A Generic - Reusable Diff Algorithm in C#.
// By aprenot 
// http://www.codeproject.com/csharp/C__Diff_Algorithm.asp

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using nDeveloper.VSS.DifferenceEngine;

namespace nDeveloper.VSS
{
    public partial class frmShowDiff : Form
    {

        public frmShowDiff(DiffList_TextFile source, DiffList_TextFile destination, ArrayList DiffLines, double seconds)
		{
			this.components = null;
			this.InitializeComponent();
			//this.Text = string.Format("Results: {0} secs.", seconds.ToString("#0.00"));
			int num1 = 1;
			foreach (DiffResultSpan span1 in DiffLines)
			{
				ListViewItem item1;
				int num2;
				switch (span1.Status)
				{
					case DiffResultSpanStatus.NoChange:
					{
						num2 = 0;
						goto Label_01DD;
					}
					case DiffResultSpanStatus.Replace:
					{
						num2 = 0;
						goto Label_035B;
					}
					case DiffResultSpanStatus.DeleteSource:
					{
						num2 = 0;
						goto Label_0114;
					}
					case DiffResultSpanStatus.AddDestination:
					{
						num2 = 0;
						goto Label_0292;
					}
					default:
					{
						continue;
					}
				}
			Label_0078:
				item1 = new ListViewItem(num1.ToString("00000"));
				ListViewItem item2 = new ListViewItem(num1.ToString("00000"));
				item1.BackColor = Color.Red;
				item1.SubItems.Add(((TextLine) source.GetByIndex(span1.SourceIndex + num2)).Line);
				item2.BackColor = Color.LightGray;
				item2.SubItems.Add("");
				this.lvSource.Items.Add(item1);
				this.lvDestination.Items.Add(item2);
				num1++;
				num2++;
			Label_0114:
				if (num2 < span1.Length)
				{
					goto Label_0078;
				}
				continue;
			Label_012D:
				item1 = new ListViewItem(num1.ToString("00000"));
				item2 = new ListViewItem(num1.ToString("00000"));
				item1.BackColor = Color.White;
				item1.SubItems.Add(((TextLine) source.GetByIndex(span1.SourceIndex + num2)).Line);
				item2.BackColor = Color.White;
				item2.SubItems.Add(((TextLine) destination.GetByIndex(span1.DestIndex + num2)).Line);
				this.lvSource.Items.Add(item1);
				this.lvDestination.Items.Add(item2);
				num1++;
				num2++;
			Label_01DD:
				if (num2 < span1.Length)
				{
					goto Label_012D;
				}
				continue;
			Label_01F6:
				item1 = new ListViewItem(num1.ToString("00000"));
				item2 = new ListViewItem(num1.ToString("00000"));
				item1.BackColor = Color.LightGray;
				item1.SubItems.Add("");
				item2.BackColor = Color.LightGreen;
				item2.SubItems.Add(((TextLine) destination.GetByIndex(span1.DestIndex + num2)).Line);
				this.lvSource.Items.Add(item1);
				this.lvDestination.Items.Add(item2);
				num1++;
				num2++;
			Label_0292:
				if (num2 < span1.Length)
				{
					goto Label_01F6;
				}
				continue;
			Label_02AB:
				item1 = new ListViewItem(num1.ToString("00000"));
				item2 = new ListViewItem(num1.ToString("00000"));
				item1.BackColor = Color.Red;
				item1.SubItems.Add(((TextLine) source.GetByIndex(span1.SourceIndex + num2)).Line);
				item2.BackColor = Color.LightGreen;
				item2.SubItems.Add(((TextLine) destination.GetByIndex(span1.DestIndex + num2)).Line);
				this.lvSource.Items.Add(item1);
				this.lvDestination.Items.Add(item2);
				num1++;
				num2++;
			Label_035B:
				if (num2 < span1.Length)
				{
					goto Label_02AB;
				}
			}
		}

        private void lvDestination_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lvDestination.SelectedItems.Count > 0)
            {
                ListViewItem item1 = this.lvSource.Items[this.lvDestination.SelectedItems[0].Index];
                item1.Selected = true;
                item1.EnsureVisible();
            }
        }

        private void lvSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.lvSource.SelectedItems.Count > 0)
            {
                ListViewItem item1 = this.lvDestination.Items[this.lvSource.SelectedItems[0].Index];
                item1.Selected = true;
                item1.EnsureVisible();
            }
        }

        private void frmShowDiff_Load(object sender, EventArgs e)
        {

        }

        // Fields
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private ColumnHeader columnHeader4;
        private Container components;
        private ListView lvDestination;
        private System.Windows.Forms.ColumnHeader columnHeader5;
        private System.Windows.Forms.ColumnHeader columnHeader6;
        private System.Windows.Forms.Button btnClose;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Splitter splitter1;
        private System.Windows.Forms.Panel panel3;
        private ListView lvSource;

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}